//待页面加载完
$(function(){
    // 定义收取收件箱的方法
    function getEmail(){
        $.post("getEmail",function (resultEntity) {
            if(resultEntity.resultFlag==true){
                var emailUnit = resultEntity.data.emailUnit;
                var length = emailUnit.length;
                var emailList="";
                var emailList0="";
                // 用来记录实际展示邮件的数量
                var emailNum=0;
                for (var i=0;i<length;i++){
                    if (resultEntity.data.emailUnit[i].subject=="后台管理系统邮箱登录通知") {
                        continue;
                    }
                    if (resultEntity.data.emailUnit[i].subject=="找回密码邮件") {
                        continue;
                    }
                    if (resultEntity.data.emailUnit[i].unread==1) {
                        continue;
                    }
                    emailList+="<li><a href=\"emailDetail.html?address="+resultEntity.data.emailUnit[i].address+"&subject="+resultEntity.data.emailUnit[i].subject+"&date="+resultEntity.data.emailUnit[i].date+"\">\n" +
                        "                    <div class=\"pull-left\"><img src=\"dist/img/img1.jpg\" class=\"img-circle\" alt=\"User Image\"></div>\n" +
                        "                    <h4>"+resultEntity.data.emailUnit[i].address+"</h4>\n" +
                        "                    <p>"+resultEntity.data.emailUnit[i].subject+"</p>\n" +
                        "                    <p><small><i class=\"fa fa-clock-o\"></i> "+resultEntity.data.emailUnit[i].date+"</small></p>\n" +
                        "                  </a></li>\n";

                    if (emailNum<3) {
                        emailList0 += "<a href=\"emailDetail.html?address=" + resultEntity.data.emailUnit[i].address + "&subject=" + resultEntity.data.emailUnit[i].subject + "&date=" + resultEntity.data.emailUnit[i].date + "\">\n" +
                            "              <div class=\"user-img pull-left\"> <img src=\"dist/img/img1.jpg\" class=\"img-circle img-responsive\" alt=\"User Image\"> <span class=\"profile-status invisable pull-right\"></span> </div>\n" +
                            "              <div class=\"mail-contnet\">\n" +
                            "                <h5>" + resultEntity.data.emailUnit[i].address + "</h5>\n" +
                            "                <span class=\"mail-desc\">" + resultEntity.data.emailUnit[i].subject + "</span> <span class=\"time\">" + resultEntity.data.emailUnit[i].date + "</span> </div>\n" +
                            "              </a> ";
                    }


                    emailNum++;
                }
                $("#emailList").html(emailList);
                $("#emailNum").text(emailNum);
                if ($("#emailNum0").length>0){
                    $("#emailNum0").text(emailNum);
                    if (emailNum<3) {
                        for (var i = 0; i <3-emailNum ; i++) {
                            emailList0+="<a href=\"#\">\n" +
                                "              <div class=\"user-img pull-left\"> <img src=\"dist/img/img1.jpg\" class=\"img-circle img-responsive\" alt=\"User Image\"> <span class=\"profile-status online pull-right\"></span> </div>\n" +
                                "              <div class=\"mail-contnet\">\n" +
                                "                <h5>-----</h5>\n" +
                                "                <span class=\"mail-desc\">---------------------------------------------</span> <span class=\"time\">--:-- AM</span> </div>\n" +
                                "              </a> "
                        }
                    }
                    $("#emailList0").html(emailList0);
                }
            }else {
                layer.msg("收件箱获取失败");
            }
        });
    }
    
    
    // 获取管理员名字和邮箱，显示在页面上
    $.post("adminName",function (resultEntity) {
        var adminName = resultEntity.data.adminname;
        var adminEmail = resultEntity.data.email;
        $(".adminName").text(adminName);
        $("#email").text(adminEmail);
        $("#emailLogin").val(adminEmail);
        if ($("#emailLogin0").length>0) {
            $("#emailLogin0").val(adminEmail);
        }
    });

    // 检查邮箱登录情况
    $.post("checkFirstLoginEmail",function (resultEntity) {
        if(resultEntity.resultFlag==true){
            $("#emailLoginButton").attr("disabled","disabled");
            $("#emailLoginButton").text("已登录");
            $("#password").val(resultEntity.data);
            $("#password").attr("disabled", "disabled");
            // 去除红点提示
            $(".noticePoint").attr("class","");
            //让写邮件和进入邮箱按钮可以使用
            $("#writeEmail").removeAttr("disabled");
            $("#enterEmail").removeAttr("disabled");
            // 已登录也进行邮件列表的展示
            getEmail();
        }
    });


    $("#emailLoginButton").click(function () {
        //获取页面的邮箱
        var sendHost = $("#emailLogin").val();
        //获取页面的邮箱验证码
        var password = $("#password").val();
        // ajax登录邮箱
        $.post("loginEmail",{"sendHost":sendHost,"password":password},function (resultEntity) {
            if (resultEntity.resultFlag==true) {
                // 登录完，登录按钮失效，显示已登录
                $("#emailLoginButton").attr("disabled", "disabled");
                $("#emailLoginButton").text("已登录");
                // 固定住验证码的输入
                $("#password").val(password);
                $("#password").attr("disabled", "disabled");
                // 去除红点提示
                $(".noticePoint").attr("class","");
                //让写邮件和进入邮箱按钮可以使用
                $("#writeEmail").removeAttr("disabled");
                $("#enterEmail").removeAttr("disabled");

                getEmail();
                // 弹窗提示登陆成功
                layer.msg('邮箱登录成功，收件箱获取中...');
            }else {
                layer.msg('邮箱登录失败');
            }
        });
    });

    // 进入写邮件页面
    $("#writeEmail").click(function () {
        window.location.replace("writeEmail.html");
    });






});
